package com.bsoft.exchange.aop;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;

/**
 * 全局日志输出
 *项目名称：exchange-framework  <br>
 *类名称：WebLogAspect <br>
 *类描述：<br>
 *创建人：ZQ <br>  
 *创建时间：2019年6月10日 下午3:26:32  <br> 
 *@version：    <br>
 *
 */
@Aspect
@Component
@Slf4j
public class WebLogAspect {

//	private static final Logger logger = LoggerFactory.getLogger(WebLogAspect.class);

	@Pointcut("execution(public * com.bsoft.exchange.controller.*.*(..)) || execution(public * com.bsoft.exchange.webservice.impl.*.*(..))")
	public void webLog() {
	}

	@Before("webLog()")
	public void doBefore(JoinPoint joinPoint) throws Throwable {
		// 接收到请求，记录请求内容 记录 最多半年数据迁移 云备份 nosql 数据库
		ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
		HttpServletRequest request = attributes.getRequest();
		// 记录下请求内容
		log.info("URL : " + request.getRequestURL().toString());
		log.info("HTTP_METHOD : " + request.getMethod());
		log.info("IP : " + request.getRemoteAddr());
		Enumeration<String> enu = request.getParameterNames();
		while (enu.hasMoreElements()) {
			String key = (String) enu.nextElement();
			log.info("key:{},value:{}", key, request.getParameter(key));
		}
		
	}

	
	@AfterReturning(returning = "ret", pointcut = "webLog()")
	public void doAfterReturning(Object ret) throws Throwable {
		// 处理完请求，返回内容123456
		log.info("RESPONSE : " + ret);
	}
}
